﻿@model IEnumerable
     
@functions{
    IEnumerable GetEditTypes() { 
        return Enum.GetValues(typeof(GridViewBatchEditMode)); 
    }
    IEnumerable GetStartEditActions() {
        return Enum.GetValues(typeof(GridViewBatchStartEditAction));
    }
}

<script type="text/javascript">
    // <![CDATA[
        function gridView_BatchEditStartEditing(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            if(!e.rowValues.hasOwnProperty(productNameColumn.index))
                return;
            var cellInfo = e.rowValues[productNameColumn.index];
            ProductNameTextBox.SetValue(cellInfo.value);

            if(e.focusedColumn === productNameColumn)
                ProductNameTextBox.SetFocus();
        }
        function gridView_BatchEditEndEditing(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            if(!e.rowValues.hasOwnProperty(productNameColumn.index))
                return;
            var cellInfo = e.rowValues[productNameColumn.index];
            cellInfo.value = ProductNameTextBox.GetValue();
            cellInfo.text = ProductNameTextBox.GetText();

            ProductNameTextBox.SetValue(null);
        }
        function gridView_BatchEditRowValidating(s, e) {
            var productNameColumn = s.GetColumnByField("ProductName");
            var cellValidationInfo = e.validationInfo[productNameColumn.index];
            if(!cellValidationInfo) return;
            var value = cellValidationInfo.value;
            if(!ASPxClientUtils.IsExists(value) || ASPxClientUtils.Trim(value) === "") {
                cellValidationInfo.isValid = false;
                cellValidationInfo.errorText = "Product Name is required";
            }
        }

        var preventEndEditOnLostFocus = false;
        function ProductNameTextBox_KeyDown(s, e) {
            var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
            if(keyCode !== ASPxKey.Tab) return;
            var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
            if(gridView.batchEditApi[moveActionName]()) {
                _aspxPreventEventAndBubble(e.htmlEvent);
                preventEndEditOnLostFocus = true;
            }
        }
        function ProductNameTextBox_LostFocus(s, e) {
            if(!preventEndEditOnLostFocus)
                gridView.batchEditApi.EndEdit();
            preventEndEditOnLostFocus = false;
        }
    // ]]>
</script>
           
@using(Html.BeginForm()) {
    <div class="vertOptionsPanel" style="padding-bottom: 15px">
        <div>
            <label for="editMode">Edit Mode:</label>
            @Html.DropDownList("editMode", new SelectList(GetEditTypes(), GridViewEditingDemosHelper.BatchEditMode), new { style = "width: 200px;" })
        </div>
        <div>
            <label for="startEditAction">Start Edit Action:</label>
            @Html.DropDownList("startEditAction", new SelectList(GetStartEditActions(), GridViewEditingDemosHelper.BatchStartEditAction), new { style = "width: 200px;" })
        </div>
        <input type="submit" value="Apply" />
    </div>
    <br />
    @Html.Partial("BatchEditingPartial", Model)
}
